package com.aliwo.controller;

import com.aliwo.entity.MessageRecord;
import com.aliwo.request.MessageRecordRequest;
import com.aliwo.response.ResultResponse;
import com.aliwo.service.MessageConsumer;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * .
 *
 * @author Xu Yayuan
 * @date 2021/11/18
 */
@RequestMapping("/api/v1/log")
@RestController
public class MessageRecordController {

    @Autowired
    private MessageConsumer messageConsumer;

    @ApiOperation("记录日志到数据库")
    @PostMapping("/insert")
    public ResultResponse loginUrl(@RequestBody MessageRecordRequest request) {
        MessageRecord messageRecord = new MessageRecord();
        BeanUtils.copyProperties(request, messageRecord);
        Integer rows = messageConsumer.sendMessage(messageRecord);
        if (1 == rows) {
            return ResultResponse.buildSuccess("记录日志成功");
        } else {
            return ResultResponse.buildError("记录日志失败");
        }
    }

}
